<?php
/**
 * Created by PhpStorm.
 * User: ybjaa
 * Date: 2017/4/20
 * Time: 16:39
 *
 * 操作用户的相对应的控制器
 */

namespace App\Http\Controllers;


use App\Business\ReturnResult;
use App\Business\Users\GetAllUsers;
use App\Business\Users\Login;
use App\Business\Users\Register;
use App\Business\Users\ResetPassword_SoucePassword;
use Illuminate\Support\Facades\Auth;

class UsersController extends Controller
{









    /**
     *
     * 获得所有用户
     *
     * @return \Illuminate\Http\JsonResponse 获得所有用户
     */
    public function getAllUsers()
    {
        return response()->json((new GetAllUsers())->GetAllUsers());
    }


    /**
     *
     * 登录
     *
     * @return mixed
     */
    public function LoginFromUserName()
    {

        $parameters=$this->getParameters();
        if(!$parameters) return json_encode(ReturnResult::ParameterError());

        $username=$parameters->userName;
        $password=$parameters->password;

        if(!$this->CheckFileds($username) || !$this->CheckFileds($password))
        {
            return json_encode(ReturnResult::Error(null,"参数错误"));
        }

        //删除所有用户名空格
        $username=str_replace(" ","",$username);


        //登录业务
        $business=new Login();

        //配置参数
        $business->userName=$username;
        $business->password=$password;

        return $business->Excute();

    }



    /**
     * 检测用户是否有效
     */
    public function CheckUser()
    {
        /*$parameters=$this->getParameters();
        if(!$parameters) return json_encode(ReturnResult::ParameterError());

        $token=$parameters->token; //取值备份，到底是否需要以后再说*/

        $id=$this->GetUserID();
        return ReturnResult::DefineResult($id??""? true:false)->ToJson();
    }


    /**
     * 登出
     * @return array
     */
    public function Logout()
    {
        /*$parameters=$this->getParameters();
        if(!$parameters) return json_encode(ReturnResult::ParameterError());

        $token=$parameters->token; //取值备份，到底是否需要以后再说*/

        $this->ClearUser();

        return ["result"=>true];
    }


    /**
     * 注册用户
     * @return ReturnResult|string
     */
    public function Regesist()
    {
        $parameters=$this->getParameters();
        if(!$parameters) return json_encode(ReturnResult::ParameterError());

        $username=$parameters->userName;
        $password=$parameters->password;

        if(!$this->CheckFileds($username) || !$this->CheckFileds($password))
        {
            return json_encode(ReturnResult::Error(null,"参数错误"));
        }

        //删除所有用户名空格
        $username=str_replace(" ","",$username);

        $business=new Register();
        $business->userName=$username;
        $business->password=$password;

        return $business->Excute();
    }

    /**
     * 修改密码
     * @return ReturnResult|string
     */
    public function ModifyPassword()
    {
        $parameters=$this->getParameters();

        //1、是否为空
        if(!$parameters||!$parameters->sourcePassword||!$parameters->newPassword)
        {
            return json_encode(ReturnResult::ParameterError());
        }

        $business=new ResetPassword_SoucePassword();
        $business->sourcePassword=$parameters->sourcePassword;
        $business->newPassword=$parameters->newPassword;

        return $business->Excute();


    }

}